<template>
	<main-content>
		<view class="content-color">
			<u-form :model="formData" ref="formDataRef" :rules='rules' label-width='auto'
				:label-style="{color: 'rgba(153, 153, 153, 1)' }">
				<view :class="{isDisabled:formType=='view'}" class="p-b-90">
					<view class="p-16">
						<group-title>
						<template #icon>
							<view class="main-color">
								<u-icon name="title-icon" custom-prefix="custom-icon" />
							</view>
						</template>
						基本信息</group-title>
					</view>
					<view class="bg-white p-r-16 p-l-16">
						<u-form-item label="商户号/支付宝账号" prop="jSubject.account"
							:required="rules['jSubject.account']?.[0].required">
							<u-input input-align='right' v-model="jSubject.account" />
						</u-form-item>
						<u-form-item label="姓名" prop="jSubject.name" :required="rules['jSubject.name']?.[0].required">
							<u-input input-align='right' v-model="jSubject.name" />
						</u-form-item>
						<u-form-item label="手机号码" prop="jSubject.phone"
							:required="rules['jSubject.phone']?.[0].required">
							<u-input input-align='right' v-model="jSubject.phone" />
						</u-form-item>
						<u-form-item label="邮箱" prop="jSubject.email" :required="rules['jSubject.email']?.[0].required">
							<u-input input-align='right' v-model="jSubject.email" />
						</u-form-item>
						<!-- <u-form-item label="产品名称" prop="image" required>
							<uni-file-picker v-model="formData.image" fileMediatype="image" mode="grid"
								:auto-upload="false" @select="selectUpload" />
						</u-form-item> -->
						<!-- <u-form-item label="产品名称" prop="productId" required>
							<my-select clearable input-align='right' v-model="formData.productId"
								:queryParams="{ channelType: formData.channelType, status: 1 }" :api="productApi.page"
								labelKey='productName' @change='changeProductName' />
						</u-form-item> -->
					</view>

					<view class="p-16">
						<group-title>
						<template #icon>
							<view class="main-color">
								<u-icon name="title-icon" custom-prefix="custom-icon" />
							</view>
						</template>
						经营信息</group-title>
					</view>
					<view class="bg-white p-r-16 p-l-16">
						<u-form-item label="主体类型" prop="jSubject.subjectType"
							:required="rules['jSubject.subjectType']?.[0].required">
							<my-select clearable input-align='right' v-model="jSubject.subjectType"
								:options="SUBJECT_TYPE" @change='changesubjectType' />
						</u-form-item>
						<u-form-item label="一级经营类目" prop="jSubject.mccCodeOne"
							:required="rules['jSubject.mccCodeOne']?.[0].required">
							<my-select clearable input-align='right' v-model="jSubject.mccCodeOne"
								:queryParams="headParams" :initParams="headInitParams" :api="businessApi.page"
								labelKey='categoryName' valueKey='categoryCode' @change='changeMccCodeOne' />
						</u-form-item>
						<u-form-item label="二级经营类目" prop="jSubject.mccCodeTwo"
							:required="rules['jSubject.mccCodeTwo']?.[0].required">
							<my-select clearable input-align='right' v-model="jSubject.mccCodeTwo"
								:queryParams="qualificationParams" :initParams="qualificationInitParams"
								:api="businessApi.page" labelKey='categoryName' valueKey='categoryCode'
								@change='changeMccCodeTwo' />
						</u-form-item>
						<u-form-item label="费率" prop="jSubject.rate" :required="rules['jSubject.rate']?.[0].required">
							<u-input input-align='right' v-model="jSubject.rate" disabled />
						</u-form-item>
						<u-form-item label="企业特殊资质图片" prop="jSubject.specialQualifications"
							:required="rules['jSubject.specialQualifications']?.[0].required">
							<view class="flex flex-row justify-end w-100">
								<fileUpload v-model="jSubject.specialQualifications" ref="fileUploadRef"
									@upload="selectUpload" :maxCount="1" />
							</view>
						</u-form-item>
						<u-form-item label="签约且授权标识" prop="jSubject.signAndAuth"
							:required="rules['jSubject.signAndAuth']?.[0].required">
							<view class="flex flex-row justify-end w-100">
								<u-switch v-model="jSubject.signAndAuth" :inactive-value="0" :active-value="1"
									size="38" />
							</view>
						</u-form-item>
						<u-form-item label="营业执照号码" prop="jSubject.businessLicenseNo"
							:required="rules['jSubject.businessLicenseNo']?.[0].required">
							<u-input input-align='right' v-model="jSubject.businessLicenseNo" />
						</u-form-item>
						<u-form-item label="营业执照法人手机号" prop="jSubject.businessLicenseMobile"
							:required="rules['jSubject.businessLicenseMobile']?.[0].required">
							<u-input input-align='right' v-model="jSubject.businessLicenseMobile" />
						</u-form-item>
						<u-form-item label="营业期限是否长期有效" prop="jSubject.longTerm"
							:required="rules['jSubject.longTerm']?.[0].required">
							<view class="flex flex-row justify-end w-100">
								<u-switch v-model="jSubject.longTerm" :inactive-value="0" :active-value="1" size="38" />
							</view>
						</u-form-item>
						<u-form-item label="营业期限" prop="jSubject.dateLimitation"
							:required="rules['jSubject.dateLimitation']?.[0].required">
							<view class="flex flex-row justify-end w-100">
								<view style="width: fit-content;">
									<uni-datetime-picker return-type='string' type="date"
										v-model="jSubject.dateLimitation" :border="false" :clearIcon='false' />
								</view>
							</view>
						</u-form-item>
						<u-form-item label="营业执照图片" prop="jSubject.businessLicensePic"
							:required="rules['jSubject.businessLicensePic']?.[0].required">
							<view class="flex flex-row justify-end w-100">
								<fileUpload v-model="jSubject.businessLicensePic" ref="fileUploadRef"
									@upload="selectUpload" :maxCount="1" />
							</view>
						</u-form-item>
						<u-form-item label="营业执照授权函" prop="jSubject.businessLicenseAuthPic"
							:required="rules['jSubject.businessLicenseAuthPic']?.[0].required">
							<view class="flex flex-row justify-end w-100">
								<fileUpload v-model="jSubject.businessLicenseAuthPic" ref="fileUploadRef"
									@upload="selectUpload" :maxCount="1" />
							</view>
						</u-form-item>

						<template v-for="(item, index) in jSceneList" :key="item?.sceneType">
							<template v-if="item?.sceneType == 0 && formData.apiType == '1'">
								<u-form-item label="营业执照法人手机号" :prop="`jSceneList.${index}.name`"
									:required="rules[`jSceneList.${index}.name`]?.[0].required">
									<u-input input-align='right' v-model="item.name" />
								</u-form-item>
								<u-form-item label="店铺地址" :prop="`jSceneList.${index}.shopAddress1`"
									:required="rules[`jSceneList.${index}.shopAddress1`]?.[0].required">
									<myArea input-align='right' clearable v-model="item.shopAddress1"
										@change="(val) => changeArea(val, item)" :getMultiple="true" />
								</u-form-item>
								<u-form-item label="详细地址" :prop="`jSceneList.${index}.url`"
									:required="rules[`jSceneList.${index}.url`]?.[0].required">
									<u-input input-align='right' v-model="item.url" />
								</u-form-item>
								<u-form-item label="店铺内景图片" :prop="`jSceneList.${index}.productUrl`"
									:required="rules[`jSceneList.${index}.productUrl`]?.[0].required">
									<view class="flex flex-row justify-end w-100">
										<fileUpload v-model="item.productUrl" ref="fileUploadRef" @upload="selectUpload"
											:maxCount="1" />
									</view>
								</u-form-item>
								<u-form-item label="店铺门头照图片" :prop="`jSceneList.${index}.indexUrl`"
									:required="rules[`jSceneList.${index}.indexUrl`]?.[0].required">
									<view class="flex flex-row justify-end w-100">
										<fileUpload v-model="item.indexUrl" ref="fileUploadRef" @upload="selectUpload"
											:maxCount="1" />
									</view>
								</u-form-item>
							</template>
						</template>

						<view class="" v-if="formData.apiType == '0'"><!--   -->
							<view class="p-y-16">
								<group-title>
						<template #icon>
							<view class="main-color">
								<u-icon name="title-icon" custom-prefix="custom-icon" />
							</view>
						</template>
						其他信息</group-title>
							</view>
							<u-form-item label="交易场景" prop="sceneTypeArr"
								:required="rules['sceneTypeArr']?.[0].required">
								<view class="flex flex-row justify-end w-100">
									<uni-data-checkbox multiple v-model="formData.sceneTypeArr"
										:localdata="scenesOptions" @change="handleSceneTypeArr" />
								</view>
							</u-form-item>

							<template v-for="(item, index) in jSceneList" :key="item?.sceneType">
								<view class="p-y-16">
									<group-title>
						<template #icon>
							<view class="main-color">
								<u-icon name="title-icon" custom-prefix="custom-icon" />
							</view>
						</template>
						{{`${selectDictLabel(SCENES_TYPE_ALI, item?.sceneType)}场景`}}</group-title>
								</view>
								<!-- 线下 -->
								<template v-if="item?.sceneType == 0">
									<view>
										<u-form-item label="店铺名称" :prop="`jSceneList.${index}.name`"
											:required="rules[`jSceneList.${index}.name`]?.[0].required">
											<u-input input-align='right' v-model="item.name" />
										</u-form-item>
										<u-form-item label="店铺地址" :prop="`jSceneList.${index}.shopAddress1`"
											:required="rules[`jSceneList.${index}.shopAddress1`]?.[0].required">
											<myArea input-align='right' clearable v-model="item.shopAddress1"
												@change="(val) => changeArea(val, item)" :getMultiple="true" />
										</u-form-item>
										<u-form-item label="详细地址" :prop="`jSceneList.${index}.url`"
											:required="rules[`jSceneList.${index}.url`]?.[0].required">
											<u-input input-align='right' v-model="item.url" />
										</u-form-item>
										<u-form-item label="店铺内景图片" :prop="`jSceneList.${index}.productUrl`"
											:required="rules[`jSceneList.${index}.productUrl`]?.[0].required">
											<view class="flex flex-row justify-end w-100">
												<fileUpload v-model="item.productUrl" ref="fileUploadRef"
													@upload="selectUpload" :maxCount="1" />
											</view>
										</u-form-item>
										<u-form-item label="店铺门头照图片" :prop="`jSceneList.${index}.indexUrl`"
											:required="rules[`jSceneList.${index}.indexUrl`]?.[0].required">
											<view class="flex flex-row justify-end w-100">
												<fileUpload v-model="item.indexUrl" ref="fileUploadRef"
													@upload="selectUpload" :maxCount="1" />
											</view>
										</u-form-item>
									</view>
								</template>
								<!-- 小程序 -->
								<template v-if="item?.sceneType == 2">
									<view>
										<u-form-item label="小程序ID" :prop="`jSceneList.${index}.appid`"
											:required="rules[`jSceneList.${index}.appid`]?.[0].required">
											<u-input input-align='right' v-model="item.appid" />
										</u-form-item>
										<u-form-item label="小程序名称" :prop="`jSceneList.${index}.name`"
											:required="rules[`jSceneList.${index}.name`]?.[0].required">
											<u-input input-align='right' v-model="item.name" />
										</u-form-item>
										<u-form-item label="小程序截图" :prop="`jSceneList.${index}.indexUrl`"
											:required="rules[`jSceneList.${index}.indexUrl`]?.[0].required">
											<view class="flex flex-row justify-end w-100">
												<fileUpload v-model="item.indexUrl" ref="fileUploadRef"
													@upload="selectUpload" :maxCount="1" />
											</view>
										</u-form-item>
									</view>
								</template>
								<!-- pc -->
								<template v-if="item?.sceneType == 3">
									<view>
										<u-form-item label="网站名称" :prop="`jSceneList.${index}.name`"
											:required="rules[`jSceneList.${index}.name`]?.[0].required">
											<u-input input-align='right' v-model="item.name" />
										</u-form-item>
										<u-form-item label="网站状态" :prop="`jSceneList.${index}.appStatus`"
											:required="rules[`jSceneList.${index}.appStatus`]?.[0].required">
											<uni-data-checkbox v-model="item.appStatus" :localdata="DEV_TYPE" />
										</u-form-item>
										<u-form-item label="网站地址" :prop="`jSceneList.${index}.url`"
											:required="rules[`jSceneList.${index}.url`]?.[0].required">
											<u-input input-align='right' v-model="item.url" />
										</u-form-item>
										<u-form-item label="可登录此网站的测试账户" :prop="`jSceneList.${index}.demoAccount`"
											:required="rules[`jSceneList.${index}.demoAccount`]?.[0].required">
											<u-input input-align='right' v-model="item.demoAccount" />
										</u-form-item>
										<u-form-item label="可以登录此网站的账户的密码" :prop="`jSceneList.${index}.demoPass`"
											:required="rules[`jSceneList.${index}.demoPass`]?.[0].required">
											<u-input input-align='right' v-model="item.demoPass" />
										</u-form-item>
										<u-form-item label="接入网址授权函" :prop="`jSceneList.${index}.authorizationUrl`"
											:required="rules[`jSceneList.${index}.authorizationUrl`]?.[0].required">
											<view class="flex flex-row justify-end w-100">
												<fileUpload v-model="item.authorizationUrl" ref="fileUploadRef"
													@upload="selectUpload" :maxCount="1" />
											</view>
										</u-form-item>
										<u-form-item label="网站首页截图" :prop="`jSceneList.${index}.indexUrl`"
											:required="rules[`jSceneList.${index}.indexUrl`]?.[0].required">
											<view class="flex flex-row justify-end w-100">
												<fileUpload v-model="item.indexUrl" ref="fileUploadRef"
													@upload="selectUpload" :maxCount="1" />
											</view>
										</u-form-item>
										<u-form-item label="网站商品页截图" :prop="`jSceneList.${index}.productUrl`"
											:required="rules[`jSceneList.${index}.productUrl`]?.[0].required">
											<view class="flex flex-row justify-end w-100">
												<fileUpload v-model="item.productUrl" ref="fileUploadRef"
													@upload="selectUpload" :maxCount="1" />
											</view>
										</u-form-item>
										<u-form-item label="网站支付页截图" :prop="`jSceneList.${index}.payUrl`"
											:required="rules[`jSceneList.${index}.payUrl`]?.[0].required">
											<view class="flex flex-row justify-end w-100">
												<fileUpload v-model="item.payUrl" ref="fileUploadRef"
													@upload="selectUpload" :maxCount="1" />
											</view>
										</u-form-item>
									</view>
								</template>
								<!-- app -->
								<template v-if="item?.sceneType == 4">
									<view>
										<u-form-item label="应用状态" :prop="`jSceneList.${index}.appStatus`"
											:required="rules[`jSceneList.${index}.appStatus`]?.[0].required">
											<uni-data-checkbox v-model="item.appStatus" :localdata="DEV_TYPE" />
										</u-form-item>
										<u-form-item label="应用名称" :prop="`jSceneList.${index}.name`"
											:required="rules[`jSceneList.${index}.name`]?.[0].required">
											<u-input input-align='right' v-model="item.name" />
										</u-form-item>
										<u-form-item label="投放市场应用" :prop="`jSceneList.${index}.appMarket`"
											:required="rules[`jSceneList.${index}.appMarket`]?.[0].required">
											<uni-data-checkbox v-model="item.appMarket" :localdata="APP_MARKET" />
										</u-form-item>
										<u-form-item label="可登录此应用的测试账户" :prop="`jSceneList.${index}.demoAccount`"
											:required="rules[`jSceneList.${index}.demoAccount`]?.[0].required">
											<u-input input-align='right' v-model="item.demoAccount" />
										</u-form-item>
										<u-form-item label="可以登录此应用的账户的密码" :prop="`jSceneList.${index}.demoPass`"
											:required="rules[`jSceneList.${index}.demoPass`]?.[0].required">
											<u-input input-align='right' v-model="item.demoPass" />
										</u-form-item>
										<u-form-item label="应用下载链接" :prop="`jSceneList.${index}.url`"
											:required="rules[`jSceneList.${index}.url`]?.[0].required">
											<u-input input-align='right' v-model="item.url" />
										</u-form-item>
										<u-form-item label="应用首页截图" :prop="`jSceneList.${index}.indexUrl`"
											:required="rules[`jSceneList.${index}.indexUrl`]?.[0].required">
											<view class="flex flex-row justify-end w-100">
												<fileUpload v-model="item.indexUrl" ref="fileUploadRef"
													@upload="selectUpload" :maxCount="1" />
											</view>
										</u-form-item>
										<u-form-item label="应用内支付页截图" :prop="`jSceneList.${index}.payUrl`"
											:required="rules[`jSceneList.${index}.payUrl`]?.[0].required">
											<view class="flex flex-row justify-end w-100">
												<fileUpload v-model="item.payUrl" ref="fileUploadRef"
													@upload="selectUpload" :maxCount="1" />
											</view>
										</u-form-item>
										<u-form-item label="商品或服务页截图" :prop="`jSceneList.${index}.productUrl`"
											:required="rules[`jSceneList.${index}.productUrl`]?.[0].required">
											<view class="flex flex-row justify-end w-100">
												<fileUpload v-model="item.productUrl" ref="fileUploadRef"
													@upload="selectUpload" :maxCount="1" />
											</view>
										</u-form-item>
										<u-form-item label="APP授权函图片" :prop="`jSceneList.${index}.authorizationUrl`"
											:required="rules[`jSceneList.${index}.authorizationUrl`]?.[0].required">
											<view class="flex flex-row justify-end w-100">
												<fileUpload v-model="item.authorizationUrl" ref="fileUploadRef"
													@upload="selectUpload" :maxCount="1" />
											</view>
										</u-form-item>
									</view>
								</template>
								<!-- 移动h5 -->
								<template v-if="item?.sceneType == 6">
									<view>
										<u-form-item label="H5网站状态" :prop="`jSceneList.${index}.appStatus`"
											:required="rules[`jSceneList.${index}.appStatus`]?.[0].required">
											<uni-data-checkbox v-model="item.appStatus" :localdata="DEV_TYPE" />
										</u-form-item>
										<u-form-item label="网站地址" :prop="`jSceneList.${index}.url`"
											:required="rules[`jSceneList.${index}.url`]?.[0].required">
											<u-input input-align='right' v-model="item.url" />
										</u-form-item>
										<u-form-item label="网站备注图片" :prop="`jSceneList.${index}.remarkUrl`"
											:required="rules[`jSceneList.${index}.remarkUrl`]?.[0].required">
											<view class="flex flex-row justify-end w-100">
												<fileUpload v-model="item.remarkUrl" ref="fileUploadRef"
													@upload="selectUpload" :maxCount="1" />
											</view>
										</u-form-item>
										<u-form-item label="H5网站首页截图" :prop="`jSceneList.${index}.indexUrl`"
											:required="rules[`jSceneList.${index}.indexUrl`]?.[0].required">
											<view class="flex flex-row justify-end w-100">
												<fileUpload v-model="item.indexUrl" ref="fileUploadRef"
													@upload="selectUpload" :maxCount="1" />
											</view>
										</u-form-item>
										<u-form-item label="H5网站支付页截图" :prop="`jSceneList.${index}.payUrl`"
											:required="rules[`jSceneList.${index}.payUrl`]?.[0].required">
											<view class="flex flex-row justify-end w-100">
												<fileUpload v-model="item.payUrl" ref="fileUploadRef"
													@upload="selectUpload" :maxCount="1" />
											</view>
										</u-form-item>
										<u-form-item label="H5网站授权函" :prop="`jSceneList.${index}.authorizationUrl`"
											:required="rules[`jSceneList.${index}.authorizationUrl`]?.[0].required">
											<view class="flex flex-row justify-end w-100">
												<fileUpload v-model="item.authorizationUrl" ref="fileUploadRef"
													@upload="selectUpload" :maxCount="1" />
											</view>
										</u-form-item>
									</view>
								</template>
							</template>
						</view>
					</view>
				</view>
				<!-- <view class="">
					<view class="p-16 flex flex-row justify-between" v-if="formType=='view'">
						<view class="w-100">
							<u-button @click="updateForm">修改</u-button>
						</view>
					</view>
					<view class="p-16 flex flex-row justify-between" v-else>
						<view class="w-30">
							<u-button @click="getHistoryData">调取进件资料</u-button>
						</view>
						<view class="w-30">
							<u-button @click="handleSave">保存</u-button>
						</view>
						<view class="w-30">
							<u-button type="primary" @click="submit">提交</u-button>
						</view>
					</view>
				</view> -->
			</u-form>
			<view class="formBtn"
				v-if="(userType == 2 && rowData.status == '0' && rowData.checkStatus == '-1')||formType=='add'">
				<view class="p-16 flex flex-row justify-between" v-if="formType=='view'">
					<view class="w-100">
						<u-button @click="updateForm">修改</u-button>
					</view>
				</view>
				<view class="p-16 flex flex-row justify-between" v-else>
					<view class="w-30">
						<u-button @click="getHistoryData">调取进件资料</u-button>
					</view>
					<view class="w-30">
						<u-button @click="handleSave">保存</u-button>
					</view>
					<view class="w-30">
						<u-button type="primary" @click="submit">提交</u-button>
					</view>
				</view>
			</view>
		</view>
	</main-content>
</template>
<script setup name='index'>
	import {
		reactive,
		ref,
		getCurrentInstance,
		computed,
		nextTick,
		toRefs,
		watch
	} from 'vue'
	import {
		onLoad,
		onShow,
		onReady
	} from '@dcloudio/uni-app'
	import {
		useCommonStore
	} from '@/stores/common.js'
	import {
		useUserStore
	} from '@/stores/user.js'
	import {
		SUBJECT_TYPE, // 主体类型
		SCENES_TYPE_ALI, //场景类型
		DEV_TYPE, //应用状态
		APP_MARKET, //应用市场
	} from '@/common/dict.js'
	import {
		debounce,
		selectDictLabel,
		parseTime,
		cloneDeep,
		parseQueryParams,
		modifyFields,
		strToArr
	} from '@/common/common.js'
	import {
		myUpload
	} from '@/pages-menus/common/myUpload.js'
	import {
		registApi
	} from '@/pages-menus/common/api/regist/index.js'
	import {
		businessApi
	} from '@/pages-menus/common/api/regist/business.js'

	import myArea from '@/components/my-area/index.vue'
	import fileUpload from '@/pages-menus/components/file-upload/index.vue'
	import {
		validatePhoneNot
	} from '@/common/validate'
	const userType = computed(() => {
		return useUserStore().userType
	})
	const commonStore = useCommonStore()
	const {
		proxy
	} = getCurrentInstance()

	const formType = ref('add')
	const rowData = ref({})

	const maiColor = computed(() => {
		return proxy.$System.maiColor.value
	})
	const formDataRef = ref(null)

	const validatorFn = (rule, value, callback) => {
		const judge = rule.judge ?? (() => {
			return true
		})
		const mes = rule.message || '不能为空'

		if (judge(value)) {
			callback(new Error(mes));
		} else {
			callback();
		}
	}

	const rules = computed(() => {
		let obj = {
			sceneTypeArr: [{
				required: false,
				validator: validatorFn,
				judge: (val) => {
					return !val || val.length === 0
				},
				message: '交易场景 不能为空!',
				trigger: ['blur', 'change']
			}], //交易场景
			'jSubject.account': [{
				required: true,
				message: '商户号/支付宝账号不能为空!',
				trigger: ['blur', 'change']
			}],
			'jSubject.account': [{
				required: true,
				message: '商户号/支付宝账号不能为空!',
				trigger: ['blur', 'change']
			}],
			'jSubject.name': [{
				required: true,
				message: '姓名不能为空!',
				trigger: ['blur', 'change']
			}],
			'jSubject.phone': [{
					required: true,
					message: '手机号码不能为空!',
					trigger: ['blur', 'change']
				},
				{
					validator: validatePhoneNot,
				},
			],
			'jSubject.subjectType': [{
				required: true,
				message: '主体类型 不能为空!'
			}],
			'jSubject.mccCodeOne': [{
				required: true,
				message: '经营类目 不能为空!',
				trigger: ['blur', 'change']
			}],
			'jSubject.mccCodeTwo': [{
				required: true,
				message: '经营类目 不能为空!',
				trigger: ['blur', 'change']
			}],
			'jSubject.specialQualifications': [{
				required: false,
				validator: validatorFn,
				judge: (val) => {
					return jSubject.value.specialLicenseType == '0' && (!val || val.length === 0)
				},
				trigger: ['blur', 'change'],
			}],
			'jSubject.businessLicenseNo': [{
				required: true,
				message: '营业执照号码 不能为空!',
				trigger: ['blur', 'change']
			}],
			'jSubject.businessLicenseMobile': [{
				required: true,
				message: '营业执照法人手机号 不能为空!',
				trigger: ['blur', 'change']
			}],
			'jSubject.businessLicensePic': [{
				required: false,
				validator: validatorFn,
				judge: (val) => {
					return jSubject.value.subjectType == '0' && (!val || val.length === 0)
				},
				trigger: ['blur', 'change'],
				message: '营业执照图片 不能为空!',
			}],
		}
		if (jSceneList.value && jSceneList.value.length > 0) {
			jSceneList.value.forEach((item, index) => {
				obj[`jSceneList.${index}.appMarket`] = [{
					required: true,
					message: '投放应用市场 不能为空!',
					trigger: ['blur', 'change']
				}] //投放应用市场 0苹果 1应用宝 2华为 3 360 4小米 5豌豆夹 6其他
				obj[`jSceneList.${index}.merchantShortName`] = [{
					required: true,
					message: '商户简称 不能为空!',
					trigger: ['blur', 'change']
				}] //商户简称
				obj[`jSceneList.${index}.servicePhone`] = [{
					required: true,
					message: '客服电话 不能为空!',
					trigger: ['blur', 'change']
				}] //客服电话
				obj[`jSceneList.${index}.indexUrl`] = [{
					required: false,
					validator: validatorFn,
					judge: (val) => {
						return (!val || val.length === 0)
					},
					message: '小程序/h5网站/应用首页/门店门头/服务或公众号页面 不能为空!',
					trigger: ['blur', 'change']
				}] //首页 小程序/h5网站/应用首页/门店门头/服务或公众号页面
				obj[`jSceneList.${index}.payUrl`] = [{
					required: false,
					validator: validatorFn,
					judge: (val) => {
						return (!val || val.length === 0)
					},
					message: 'h5网站/应用支付页/网站支付页/企微页面 不能为空!',
					trigger: ['blur', 'change']
				}] //支付 h5网站/应用支付页/网站支付页/企微页面
				obj[`jSceneList.${index}.demoAccount`] = [{
					required: false,
					message: '测试用户 不能为空!',
					trigger: ['blur', 'change']
				}] //测试用户
				obj[`jSceneList.${index}.appStatus`] = [{
					required: true,
					message: '应用状态 不能为空!',
					trigger: ['blur', 'change']
				}] //应用状态 0未上线 1已上线
				obj[`jSceneList.${index}.url`] = [{
					required: false,
					validator: validatorFn,
					judge: (val) => {
						return item.sceneType == '0' && (!val || val.length === 0)
					},
					message: '网站地址/门店详细地址/应用下载链接 不能为空!',
					trigger: ['blur', 'change']
				}] //网站地址/门店详细地址/应用下载链接
				obj[`jSceneList.${index}.sId`] = [{
					required: true,
					message: '商户经营信息表编号 不能为空!',
					trigger: ['blur', 'change']
				}] //商户经营信息表编号
				obj[`jSceneList.${index}.authorizationUrl`] = [{
					required: false,
					message: '授权 不能为空!',
					trigger: ['blur', 'change']
				}] //授权 h5网站授权/app授权/接入网址授权
				obj[`jSceneList.${index}.merchantId`] = [{
					required: true,
					message: '商户编号 不能为空!',
					trigger: ['blur', 'change']
				}] //商户编号
				obj[`jSceneList.${index}.productUrlM`] = [{
					required: false,
					validator: validatorFn,
					judge: (val) => {
						return (!val || val.length === 0)
					},
					message: '图片 不能为空!',
					trigger: ['blur', 'change']
				}] //商品 商品服务/网站商品页/店铺内景
				obj[`jSceneList.${index}.appid`] = [{
					required: true,
					message: 'appID 不能为空!',
					trigger: ['blur', 'change']
				}] //商户appID/企微corpId
				obj[`jSceneList.${index}.name`] = [{
					required: true,
					message: '名称 不能为空!',
					trigger: ['blur', 'change']
				}] //小程序名称/应用名称/门店名称/网站名称
				obj[`jSceneList.${index}.demoPass`] = [{
					required: false,
					message: '测试密码 不能为空!',
					trigger: ['blur', 'change']
				}] //测试密码
				obj[`jSceneList.${index}.shopProvinceCode`] = [{
					required: true,
					message: ' 不能为空!',
					trigger: ['blur', 'change']
				}] //省份
				obj[`jSceneList.${index}.shopCityCode`] = [{
					required: true,
					message: ' 不能为空!',
					trigger: ['blur', 'change']
				}] //市
				obj[`jSceneList.${index}.shopDistrictCode`] = [{
					required: true,
					message: ' 不能为空!',
					trigger: ['blur', 'change']
				}] //区县
				obj[`jSceneList.${index}.shopAddress1`] = [{
					required: true,
					message: '地址 不能为空!'
				}] //地址
				obj[`jSceneList.${index}.remarkUrl`] = [{
					required: true,
					message: '备注 不能为空!',
					trigger: ['blur', 'change']
				}] //备注
				obj[`jSceneList.${index}.id`] = [{
					required: true,
					message: '编号 不能为空!',
					trigger: ['blur', 'change']
				}] //编号
				obj[`jSceneList.${index}.productUrl`] = [{
					required: false,
					validator: validatorFn,
					judge: (val) => {
						return (!val || val.length === 0)
					},
					message: '商品 不能为空!',
					trigger: ['blur', 'change']
				}] //商品 商品服务/网站商品页
			})
		}
		return obj
	})

	// 图片字段
	const imgField = [
		'specialQualifications',
		'qualifications',
		'businessLicensePic',
		'financeLicensePics',
		'businessLicenseAuthPic',
		'indexUrl',
		'remarkUrl',
		'payUrl',
		'authorizationUrl',
		'productUrl',
		'idDocCopy',
		'idDocCopyBack',
		'authorizeLetterCopy',
		'businessAuthorizationLetter',
		'legalPersonCommitment',
		'legalPersonVideo',
		'businessAdditionPics',
	]
	// 场景子表字段
	const jSceneListChild = {
		appMarket: undefined, //投放应用市场 0苹果 1应用宝 2华为 3 360 4小米 5豌豆夹 6其他
		merchantShortName: undefined, //商户简称
		servicePhone: undefined, //客服电话
		demoAccount: undefined, //测试用户
		type: undefined, //平台类型 0支付宝 1微信
		appStatus: undefined, //应用状态 0未上线 1已上线
		url: undefined, //网站地址/门店详细地址/应用下载链接
		sId: undefined, //商户经营信息表编号
		merchantId: undefined, //商户编号
		sceneType: undefined, //场景类型 0线下 1服务号和公众号 2小程序 3pc 4app 5企业微信 6移动h5
		productUrlM: undefined, //商品 商品服务/网站商品页/店铺内景
		appid: undefined, //商户appID/企微corpId
		name: undefined, //小程序名称/应用名称/门店名称/网站名称
		shopCityCode: undefined, //市
		demoPass: undefined, //测试密码
		shopDistrictCode: undefined, //区县
		id: undefined, //编号
		shopProvinceCode: undefined, //省份
		shopAddress1: [], //合并地址
		indexUrl: [], //首页 小程序/h5网站/应用首页/门店门头/服务或公众号页面
		remarkUrl: [], //备注
		payUrl: [], //支付 h5网站/应用支付页/网站支付页/企微页面
		authorizationUrl: [], //授权 h5网站授权/app授权/接入网址授权
		productUrl: [], //商品 商品服务/网站商品页
	}
	const initData = {
		mpdId: undefined, //商户进件关联编号
		jBankAccounts: {}, //结算银行账户表
		jSubject: {
			financeInstitution: undefined, //是否为金融机构 0否1是
			businessLicenseMobile: undefined, //营业执照法人手机号
			merchantShortName: undefined, //商户简称
			dateLimitation: undefined, //营业期限
			settlementId: undefined, //结算规则ID
			subjectType: undefined, //主体类型： 0: 营业执照上的主体类型一般为个体户、个体工商户、个体经营 1: 营业执照上的主体类型一般为有限公司、有限责任公司 2: 包括各级、各类政府机关，如机关党委、税务、民政、人社、工商、商务、市监等 3:包括国内各级、各类政府机构、事业单位等。 如：公安、党团、司法、交通、旅游、工商税务、市政、医疗、教育、学校等机构 4: 包括社会团体、民办非企业、基金会、基层群众性自治组织、农村集体经济组织等组织
			mccCode: undefined, //经营类目
			mccCodeOne: undefined, //经营类目
			mccCodeTwo: undefined, //经营类目
			activitiesAdditions: undefined, //活动附加说明 用,拼接
			qualificationType: undefined, //所属行业类型
			qualifications: [], //特殊资质说明 用,拼接
			signAndAuth: 1, //签约且授权标识，默认为false，只进行签约操作； 如果设置为true，则表示签约成功后，会自动进行应用授权操作。
			longTerm: 1, //营业执照是否长期有效 0否 1是
			activitiesId: undefined, //活动ID
			merchantId: undefined, //自定义商户编号
			rate: undefined, //服务费率（%），0.38~0.6 之间（小数点后两位，可取0.38%及0.6%）。 当签约且授权标识 sign_and_auth=true 时，该费率信息必填
			activitiesRate: undefined, //活动费率
			legalPersonVideo: undefined, //法人开户意愿视频 1、建议法人按如下话术录制“法人开户意愿视频”： 我是#公司全称#的法定代表人（或负责人），特此证明本公司申请的商户号为我司真实意愿开立且用于XX业务（或XX服务）。我司现有业务符合法律法规及腾讯的相关规定。 2、支持上传20M内的视频，格式可为avi、wmv、mpeg、mp4、mov、mkv、flv、f4v、m4v、rmvb； 3、请填写通过视频上传API预先上传视频生成好的MediaID 。
			id: undefined, //编号
			debitActivitiesRate: undefined, //借记卡活动费率
			creditActivitiesRate: undefined, //信用卡活动费率
			legalPersonCommitment: undefined, //法人开户承诺函1、请上传法定代表人或负责人亲笔签署的开户承诺函扫描件（下载模板）。亲笔签名承诺函内容清晰可见，不得有涂污，破损，字迹不清晰现象； 2、请填写通过图片上传API预先上传图片生成好的MediaID
			email: undefined, //邮箱
			specialLicenseType: undefined, //特殊资质类型
			certificateLetterCopy: undefined, //单位证明函照片
			businessLicenseAddress: undefined, //营业执照的注册地址，若该字段未填写，系统将会查询国家工商信息填入。需注意若工商信息查询不到，则会被审核驳回。
			servicePhone: undefined, //服务电话
			businessAdditionMsg: undefined, //补充说明
			businessLicenseName: undefined, //营业执照商户名称
			businessLicenseNo: undefined, //营业执照号码
			businessAdditionPics: undefined, //补充材料 1、最多可上传5张照片； 2、请填写通过图片上传API预先上传图片生成好的MediaID。
			phone: undefined, //手机号
			name: undefined, //姓名
			financeType: undefined, //金融机构类型 为金融机构时必传
			storeInfo: undefined, //销售场景
			account: undefined, //支付宝账号
			specialQualifications: [], //企业特殊资质图片
			businessLicensePic: [], //营业执照图片
			financeLicensePics: [], //金融机构许可证图片 用,拼接
			businessLicenseAuthPic: [], //营业执照授权函
		}, //商户经营信息表出参
		apiType: 1, //接口类型 0通用 1当面付
		jIdentitiesList: [], //商户身份信息
		jSceneList: [], //商户场景信息
		sceneTypeArr: [], //场景类型
	}
	const formData = reactive(cloneDeep(initData))
	const {
		jBankAccounts,
		jSubject,
		jSceneList,
		sceneTypeArr
	} = toRefs(formData)

	//保存
	const handleSave = async () => {
		const param = {
			...cloneDeep(formData),
		}
		imgInit(param)
		if (formType.value != 'view') {
			try {
				let res = await registApi.save(param)
				uni.showToast({
					title: `保存成功！`,
					icon: 'success',
					duration: 2000
				});
				return res
			} catch (error) {
				console.error(error)
			}
		}
	}
	// 提交
	const submit = async () => {
		formDataRef.value.validate((valid) => {
			if (valid) {
				handleSave().then(() => {
					const param = {
						checkStatus: 0,
						id: rowData.value.id
					}
					registApi.submit(param).then(() => {
						uni.showToast({
							title: `提交成功！`,
							icon: 'success',
							duration: 2000
						});
						proxy.$System.goTo('/pages-menus/pages/regist/index', {}, {
							method: 'reLaunch'
						})
						resetForm()
					})
				})
			}
		})
	}

	//调用进件资料
	function getHistoryData() {
		SmartLoading.show()
		let p = {
			id: rowData.value.id,
			channelType: rowData.value.channelType
		}
		registApi
			.findIdByChannelType(p)
			.then((res) => {
				// jSubject/jSceneList
				if (proxy.$System.isEmpty(res?.data)) return
				let resData = res.data
				imgInit(resData)
				mergeArea(resData)
				Object.assign(formData, resData)
				getSceneTypeArr()
				initFormList()
			})
			.finally(() => {
				SmartLoading.hide()
			})
	}

	const resetForm = () => {
		// 重置表单的 UI 状态
		try {
			formDataRef.value.resetFields()
		} catch (error) {
			console.error(error)
		}
		// 重置表单数据
		Object.assign(formData, cloneDeep(initData))
	}

	const getForm = (data) => {
		let p = {
			mpdId: data.id,
			type: data.channelType
		}
		registApi.findId(p).then((res) => {
			if (res.code != 200) return
			let resData = res.data
			resData.mpdId = data.id
			imgInit(resData)
			mergeArea(resData)
			Object.assign(formData, resData)
			if (resData.apiType == 1 && !resData.jSceneList.some((item) => item.sceneType == 0)) {
				// 当面付线下场景处理
				initFormList()
			}
			getSceneTypeArr()
		}).catch((err) => {
			console.error(err)
			initFormList()
		})
	}
	const titleMap = {
		add: '新增支付宝资料',
		update: '修改支付宝资料',
		view: '查看支付宝资料',
	}

	const updateForm = () => {
		formType.value = 'update'
		// 动态设置标题
		uni.setNavigationBarTitle({
			title: titleMap[formType.value]
		})
	}

	// 选择商户===》
	const changeMch = (val) => {
		console.log('选择商户===>', val);
		// if (!val[0]) {
		// 	formData.value.merchantId = undefined
		// 	return
		// }
		// formData.value.merchantId = data[0].id
	}

	const limitUpload = {
		fileType: ['.jpg', '.jpeg', '.png', '.bmp', '.gif'],
		fileSize: 5,
	}
	const beforeUpload = (file, fileList, type) => {
		// 校检文件类型
		if (limitUpload.fileType.length) {
			const fileExt = file.name.substring(file.name.lastIndexOf('.')).toLowerCase()
			const isTypeOk = limitUpload.fileType.includes(fileExt)
			if (!isTypeOk) {
				uni.showToast({
					title: `文件格式不正确, 请上传${limitUpload.fileType.toString().replace(/\./g, '').replace(/,/g, '、')}格式文件!`,
					icon: 'error',
					duration: 2000
				});
				return false
			}
		}

		// 校检文件大小
		if (limitUpload.fileSize) {
			const isLt = file.size / 1024 / 1024 < limitUpload.fileSize
			if (!isLt) {
				uni.showToast({
					title: `上传文件大小不能超过 ${limitUpload.fileSize} MB!`,
					icon: 'error',
					duration: 2000
				});
				return false
			}
		}

		return true
	}

	// 上传文件自定义的方法
	const uploadFunc = async (file, fieldName) => {
		// 确保获取到文件对象
		const fileObj = file
		if (!fileObj) {
			return
		}
		const response = await myUpload(file)
		console.log('response==>', response)

		// let arr = getObjValue(formData, fieldName).map((item) => {
		// 	if (item.uid === fileObj.uid) {
		// 		uni.showToast({
		// 			title: `${fileObj.name} 上传成功`,
		// 			icon: 'success',
		// 			duration: 2000
		// 		});
		// 		return response
		// 	}
		// 	return item
		// })
		// setObjValue(formData, fieldName, arr)
	}
	const fileUploadRef = ref(null)
	const selectUpload = async (e) => {
		console.log('选择文件：', e)
		if (e.length > 0) {
			e.forEach(async fileItem => {
				if (beforeUpload(fileItem)) {
					let res = await uploadFunc(fileItem)
				} else {
					console.log('选择文件错误：', fileItem)
					fileItem.onError()
				}
			})
		}

	}

	let areaFieldArr = ['shopProvinceCode', 'shopCityCode', 'shopDistrictCode']
	// 地址拆分
	const changeArea = (val, data) => {
		if (Array.isArray(val) && val.length > 0) {
			val.forEach((item, index) => {
				data[areaFieldArr[index]] = item
			})
		} else {
			areaFieldArr.forEach(item => {
				data[item] = ''
			})
		}
	}
	// 地址合并
	function mergeArea(data) {
		data.jSceneList.forEach((item) => {
			if (item.sceneType == '0') {
				for (let i = 0; i <= areaFieldArr.length; i++) {
					if (!item[areaFieldArr[i]]) {
						item.shopAddress1 = item[areaFieldArr[i - 1]]
					}
				}
			}
		})
	}
	// 图片初始化
	function imgInit(resData) {
		modifyFields(resData, imgField, (value, key) => {
			let val
			if (proxy.$System.isEmpty(value)) return
			if (typeof value === 'string') {
				val = strToArr(value).map((item, index) => {
					return {
						uid: `db-file-${index}`,
						name: `${key}${index > 0 ? '_' + index : ''}`,
						status: 'done',
						extname: 'text',
						url: item,
					}
				})
			}
			if (Array.isArray(value)) {
				val = value.map((e) => {
					let url = ''
					if (e?.res?.url || e?.res?.data?.url) {
						url = e.res.url || e.res.data.url
					} else {
						url = e.url
					}
					return url
				}).toString()
			}
			return val
		})
	}

	// 获取场景列表--场景初始化
	function getSceneTypeArr() {
		if (!proxy.$System.isEmpty(jSceneList.value) && jSceneList.value.length > 0) {
			sceneTypeArr.value = [...new Set(jSceneList.value.map((item) => item.sceneType))]
		} else {
			sceneTypeArr.value = []
		}
	}
	// 获取场景类型
	const handleSceneTypeArr = (val) => {
		sceneChange()
	}
	const sceneChange = () => {
		let sceneArr = jSceneList.value.map((item) => item?.sceneType)
		// formData.jSceneList.push({ ...cloneDeep(jSceneListChild), sceneType: 0 })
		formData.sceneTypeArr.forEach((item) => {
			if (!sceneArr.includes(item)) {
				formData.jSceneList.push({
					...cloneDeep(jSceneListChild),
					sceneType: item,
					type: rowData.value?.channelType
				})
			}
		})
		sceneArr.forEach((item, index) => {
			if (!formData.sceneTypeArr.includes(item)) {
				jSceneList.value.splice(index, 1)
			}
		})
	}

	/* 经营类目 */
	// 主体类型
	const headParams = computed(() => {
		return {
			pCategoryCode: '000000',
		}
	})
	const headInitParams = computed(() => {
		return {
			categoryCode: jSubject.value.mccCodeOne,
		}
	})
	const qualificationParams = computed(() => {
		return {
			pCategoryCode: jSubject.value.mccCodeOne ? jSubject.value.mccCodeOne : 'unknown',
		}
	})
	const qualificationInitParams = computed(() => {
		return {
			categoryCode: jSubject.value.mccCodeTwo,
		}
	})
	// 主体类目
	const changesubjectType = (val) => {}
	// 切换行业一级类目
	const changeMccCodeOne = (val) => {
		console.log('一级类目==》', val);
		jSubject.value.mccCodeTwo = undefined
	}
	// 切换行业二级类目
	const changeMccCodeTwo = (val) => {
		console.log('二级类目==>', val)
		jSubject.value.rate = val[0].rate
		jSubject.value.specialLicenseType = val[0].qualificationsType // 是否上传特殊资质
	}

	/* 经营类目 */

	/* 必填场景 */
	const requiredScene = computed(() => {
		return []
	})
	const scenesOptions = computed(() => {
		return SCENES_TYPE_ALI.map((item) => {
			return {
				...item,
				disabled: requiredScene.value.includes(item.value),
				label: item.label,
				text: item.label,
				value: item.value,
			}
		})
	})

	/* 数组表单初始化 */
	function initFormList() {
		// 必填场景处理
		// requiredScene.value.forEach((item) => {
		//   if (formData.jSceneList.some((item) => item.sceneType == item)) return
		//   formData.jSceneList.push({ ...cloneDeep(jSceneListChild), sceneType: item, type: rowData.value?.channelType })
		// })
		if (formData.jSceneList.some((item) => item.sceneType == 0)) return
		formData.jSceneList.push({
			...cloneDeep(jSceneListChild),
			sceneType: 0,
			type: rowData.value?.channelType
		})
		getSceneTypeArr()
	}

	watch([() => rowData.value, () => formType.value], () => {
		formData.apiType = rowData.value.apiType
		if (rowData.value?.apiType) {
			formData.apiType = rowData.value.apiType
		}
		if (rowData.value?.id) {
			formData.mpdId = rowData.value?.id
		}
		if ((formType.value == 'update' || formType.value == 'view')) {
			getForm(cloneDeep(rowData.value))
		}
		if (formType.value == 'add') {
			initFormList()
		}
	}, {
		deep: true,
		immediate: true
	})
	watch(() => rules.value, () => {
		nextTick(() => {
			formDataRef.value?.setRules(rules.value);
		})
	}, {
		deep: true,
		immediate: true
	})

	onLoad(async (opt) => {
		let options = parseQueryParams(opt)
		formType.value = options.formType ?? 'add'
		if (!proxy.$System.isEmpty(options.rowData)) {
			rowData.value = options.rowData
		}
		// 动态设置标题
		uni.setNavigationBarTitle({
			title: titleMap[formType.value]
		})
	})
	onReady(() => {
		nextTick(() => {
			formDataRef.value?.setRules(rules.value);
		})
		// paging.value.reload()
	})
</script>
<style lang='scss' scoped>
	.view-group {
		background-color: #fff;
	}

	.isDisabled {
		position: relative;
	}

	.isDisabled:before {
		position: absolute;
		content: '';
		display: block;
		width: 100%;
		height: 100%;
		background-color: rgba(25, 25, 25, .05);
		z-index: 9;
	}

	.formBtn {
		position: fixed;
		bottom: 0;
		width: 100%;
		z-index: 999;
		background-color: #fff;
		box-shadow: 0 -3px 6px #e1e1e1;
	}

	.operate-box {
		position: fixed;
		z-index: 99;
		bottom: 50rpx;
	}

	.tar {
		:deep(.u-input) {
			text-align: right !important;
		}
	}
</style>